package com.xuan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuan.entity.UserDTO;
import com.xuan.oath.client.api.app.UserRemoteApi;
import com.xuan.service.AuthUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author Lenovo
 * @description: TODO
 * @date 2021/7/23 14:35
 */
@RestController
@RequestMapping("/auth/user")
@Slf4j
public class AuthUserController implements UserRemoteApi {

    @Autowired
    private AuthUserService authUserService;


    @GetMapping("/get/{username}")
    public List<UserDTO> getUserByUserName(@PathVariable("username") String username){

        List<UserDTO> users = authUserService.list(new QueryWrapper<UserDTO>().eq("username", username));

        users.get(0).setRoles(getRoles(users.get(0).getRole()));
        return users;
    }

    private List<String> getRoles(String role){
        String[] split = role.split(",");
        return Arrays.asList(split);
    }
}
